<?php
/**
 * Owl HTML API
 * This class contains methods for generating HTML elements such as img tag,
 * anchor tag...
 *
 * @package OWL
 * @subpackage Helper
 * @author KeiMOon
 * @since 1.0
 */

class Owl_Html {
    /**
     * Make inputed string bold by adding &lt;b&gt; tag to it.
     *
     * @since 1.0
     * @param string $string The string wants to be bolded
     * @return string Bolded string
     * @author KeiMOon
     */
    static function make_it_bold($string) {
        if (strpos($string,"<b>")===false)
            return "<b>$string</b>";
        else
            return $string;
    }

    /**
     * Make inputed string align center.
     *
     * @since 1.0
     * @param string $string The string wants to be aligned center
     * @return string Aligned center string
     * @author KeiMOon
     */
    static function make_it_center($string) {
        if (strpos($string,"<div>")===false)
            return "<div style=\"text-align:center;\">$string</div>";
        else
            return $string;
    }

    /**
     * Make an approriate image link to display itself. 
     *
     * @since 1.0
     * @param string $url The URL of the image
     * @param string $alt Alternate text and tooltip of the image.
     * @param int $width Specific width of the image. This parameter is optional.
     * @param int $height Specific height of the image. This parameter is optional.
     * @return string String that contain &lt;img&gt; tag
     * @author KeiMOon
     */
    static function make_it_image($url, $alt="", $width = false, $height = false) {
        if (strpos($url,"<img>") === false) {
            $alt_text = (empty($alt)) ? "" : "alt=\"$alt\" title=\"$alt\"";
            $width_text = $height_text = "";
            if ($width || $height) {
                if (!preg_match('/https?:/i', $url)) {
                    $real_url = $_SERVER["DOCUMENT_ROOT"] . $url;
                }
                else {
                    $real_url = $url;
                }
                $size = @getimagesize($real_url);
                if ($size !== false) {
                    if ($width) {
                        $width = ($width > $size[0]) ? $size[0] : $width;
                        $width_text = "width = \"$width\"";
                    }
                    if ($height) {
                        $height = ($height > $size[1]) ? $size[1] : $height;
                        $height_text = "height = \"$height\"";
                    }
                }
                else {
                    if ($width) {
                        $width_text = "width = \"$width\"";
                    }
                    if ($height) {
                        $height_text = "height = \"$height\"";
                    }
                }
                
                
            }

            return "<img src=\"$url\" $alt_text $width_text $height_text />";
        }
        else
            return $url;
    }

    /**
     * Make a data list in the form of an array into an unorder list
     *
     * @param Array $data The data of the list
     * @return string The string that became an unorder list
     */
    static function make_it_list($data, $unordered = true) {
        $list_tyle = ($unordered) ? 'ul' : 'ol';
        $data_list = "<$list_tyle>\n";
        foreach ($data as $datum) {
            $data_list .= "<li>$datum</li>\n";
        }
        $data_list .= "</$list_tyle>";
        return $data_list;
    }

    /**
     * Create an anchored text
     *
     * @since 1.0
     * @param string $url The URL
     * @param string $title The text
     * @return string Anchored text
     */
    static function make_it_link($url, $title) {
        if (strpos($url,"<a>")===false)
            return "<a href=\"$url\" title=\"$title\">$title</a>";
        else
            return $url;
    }

    /**
     * Build pagination string. This method use the page parameter in the 
     * query string
     *
     * @param string $url The url without querystring.
     * @param string $query_string The query string
     * @param int $page_count Number of page
     * @param int $current_page The current page
     */
    static function build_pagination($page_count, $current_page) {
        echo "<div>\n";
        echo "Bạn đang ở trang $current_page trên tổng số $page_count trang.\n";
        echo "</div>\n";
        if ($page_count > 1) {
            echo "<div>\n";
            echo "\t<span>Trang </span>\n";
            $pagination_string = "";
            for ($i = 1; $i <= $page_count; $i++) {
                if ($i == $current_page) {
                    $pagination_string .= "\t<span> $i - </span>\n";
                }
                else {
                    $url = Owl_Functions::add_query_arg("page", $i);
                    $pagination_string .= "\t<span><a href='$url'> $i </a> - </span>\n";
                }
            }
            $pagination_string = preg_replace('/-\s+<\/span>\s+$/i', '</span>', $pagination_string);
            echo $pagination_string;
            echo "</div>\n";
        }
    }
    static function checked( $checked, $current = true, $echo = true) {
	return self::checked_selected_helper( $checked, $current, $echo, 'checked' );
    }
    static function selected( $selected, $current = true, $echo = true) {
            return self::checked_selected_helper( $selected, $current, $echo, 'selected' );
    }
    static function checked_selected_helper( $helper, $current, $echo, $type) {
            if ( $helper == $current)
                    $result = " $type='$type'";
            else
                    $result = '';

            if ($echo)
                    echo $result;

            return $result;
    }
}


